<?php
declare (strict_types = 1);

namespace app\command\rabbitmq;

use app\service\ProductService;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use kdy\rabbitmq\Rabbitmq;
use think\facade\Log;

class UpdateProductConsume extends Command
{
    protected $connection = 'kdy';

    protected $exchange = 'shop';

    protected $queue = 'update_product';

    protected $mq;

    protected function configure()
    {
        // 指令配置
        $this->setName('queue:update_product')
            ->setDescription('ES商品修改队列');
    }

    protected function execute(Input $input, Output $output)
    {
        //链式调用
        Rabbitmq::instance()->onConnection($this->connection)
            ->onExchange($this->exchange)
            ->onQueue($this->queue)
            ->accept(function($msg){
                echo $msg->body."\n";
                //记录消息到日志
                Log::info($msg->body);
                //业务处理
                $this->handle(json_decode($msg->body,true));
                //手动应答
                $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
          });
    }

    public function handle($data=[])
    {
        $productService = ProductService::instance();
        $productService->updateProduct($data['goods_id'],$data['data_source']);
    }

}
